﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsEarlySetDAL.cs
/// 作  者：Kidy
/// 时  间：2006.11.30
/// 说  明：ClsEarlySetDAL类用于初期设置信息相关处理
/// </summary>
public class ClsEarlySetDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取tbhTranM主表信息(有：其他入库：04、其他出库：06、期初产品：02、产品报损：10)
    /// </summary>
    /// <param name="Co_ID">公司ID</param>
    /// <param name="User_ID">用户ID</param>
    /// <param name="Ds_ID">数据集ID</param>
    /// <returns>返回采购入库或退货主表信息数据集</returns>
    public DataSet GetOtherEarlySetMains(string sTm_Type)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GettbhTranM", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@tm_type", sTm_Type);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 获取所有其他入库或其他出库信息
    /// <param name="No">ID号</param>
    /// </summary>
    /// <returns>返回采购入库或退货从表信息数据集</returns>
    public DataSet GetOtherEarlySetChilds(string Td_Type, string Tm_No)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GettbhTranD", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@tm_no", Tm_No);
        dat.SelectCommand.Parameters.Add("@td_type", Td_Type);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }


    /// <summary>
    /// 判断是否可以确认：明细表有记录则可以确认，反之不可以确认
    /// </summary>
    /// <param name="Tm_No">单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Auditor">操作员</param>
    /// <returns>操作是否成功</returns>
    public string WhetherAuditOtherEarlySet(string sTm_Type, string sTm_No)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_WhetherAuditTbhTranM", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = sTm_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = sTm_No;

        try
        {
            con.Open();
            string strReturn = (string)com.ExecuteScalar();
            return strReturn;
        }
        catch
        {
            return null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 改变单的状态
    /// </summary>
    /// <param name="Tm_No">单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Auditor">操作员</param>
    /// <returns>操作是否成功</returns>
    public Boolean AuditOtherEarlySetState(string Tm_No, string Tm_Type, int State, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AudittbhTranM_Store", con);//Pro_UpdatetbhTranMState  Pro_AudittbhTranM
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = Tm_Type;
        com.Parameters.Add("@dm_state", SqlDbType.Int, 4);
        com.Parameters["@dm_state"].Value = State;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    ///  修改
    /// </summary>
    /// <param name="Tm_No">单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Auditor">操作员</param>
    /// <returns>操作是否成功</returns>
    public Boolean UpdateOtherEarlySet(string sTm_type, string sTm_No, DateTime dtInto_date, string sInto_type, string sTm_dept, string sTm_Oper, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdatetbhTranM_M", con);//Pro_UpdatetbhTranMState
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = sTm_type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = sTm_No;
        com.Parameters.Add("@Into_date", SqlDbType.DateTime, 8);
        com.Parameters["@Into_date"].Value = dtInto_date;
        com.Parameters.Add("@Into_type", SqlDbType.NVarChar, 30);
        com.Parameters["@Into_type"].Value = sInto_type;
        com.Parameters.Add("@Tm_dept", SqlDbType.NVarChar, 30);
        com.Parameters["@Tm_dept"].Value = sTm_dept;
        com.Parameters.Add("@Tm_Oper", SqlDbType.NVarChar, 30);
        com.Parameters["@Tm_Oper"].Value = sTm_Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    ///  修改：方法重载（期初产品）
    /// </summary>
    /// <param name="Tm_No">单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Auditor">操作员</param>
    /// <returns>操作是否成功</returns>
    public Boolean UpdateOtherEarlySet(string sTm_type, string sTm_No, string sCr_id, DateTime dWrite_date, string sTm_dept, string sTm_Oper, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdatetbhTranM_M", con);//Pro_UpdatetbhTranMState
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = sTm_type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = sTm_No;
        com.Parameters.Add("@Into_date", SqlDbType.DateTime, 8);//制单日期(因为四个功能都用到同一个SP，所以参数名一样)
        com.Parameters["@Into_date"].Value = dWrite_date;
        com.Parameters.Add("@Cr_id", SqlDbType.NVarChar, 30);
        //com.Parameters["@Cr_id"].Value = sCr_id;  
        com.Parameters["@Cr_id"].Value = "C_2006104155146";//-------------------------------------------------------暂时用 
        com.Parameters.Add("@Tm_dept", SqlDbType.NVarChar, 30);
        com.Parameters["@Tm_dept"].Value = sTm_dept;
        com.Parameters.Add("@Tm_Oper", SqlDbType.NVarChar, 30);//录入人
        com.Parameters["@Tm_Oper"].Value = sTm_Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;
        com.Parameters.Add("@Into_type", SqlDbType.NVarChar, 30);
        com.Parameters["@Into_type"].Value = "";

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废其他入库主表信息
    /// </summary>
    /// <param>其他入库编号</param>
    /// <param name="Is_Visible">作废状态1为临时作废，－1为彻底作废</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteOtherEarlySet(string sTm_type, string sTm_no, int iIs_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteOtherStockInM", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = sTm_type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 20);
        com.Parameters["@tm_no"].Value = sTm_no;
        com.Parameters.Add("@Is_Visible", SqlDbType.Int, 1);
        com.Parameters["@Is_Visible"].Value = iIs_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废子单
    /// </summary>
    /// <param name="Tm_No"></param>
    /// <param name="Td_Type"></param>
    /// <param name="Im_No"></param>
    /// <param name="State"></param>
    /// <returns></returns>
    public Boolean DeleteChildOtherEarlySet(string sTm_Type, string sTm_No, string sIm_No, int iCommandName)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeletetbhTranD", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = sTm_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = sTm_No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = sIm_No;
        com.Parameters.Add("@CommandName", SqlDbType.Int, 2);
        com.Parameters["@CommandName"].Value = iCommandName;
        //com.Parameters.Add("@dm_state", SqlDbType.Int);
        //com.Parameters["@dm_state"].Value = State;


        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 增加其他入库信息
    /// </summary>
    /// <param>其他入库编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    ///strTm_no, dtInto_date, strInto_type,strTm_dept, strTm_Oper, strRemark
    public Boolean AddOtherEarlySet(string sTm_type, string sTm_No, DateTime dtInto_date, string sInto_type, string sTm_dept, string sTm_Oper, string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddtbhTranM_M", con);//Pro_AddtbhTranM
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@tm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@tm_type"].Value = sTm_type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = sTm_No;
        com.Parameters.Add("@Into_date", SqlDbType.DateTime, 8);
        com.Parameters["@Into_date"].Value = dtInto_date;
        com.Parameters.Add("@Into_type", SqlDbType.NVarChar, 30);
        com.Parameters["@Into_type"].Value = sInto_type;
        com.Parameters.Add("@Tm_dept", SqlDbType.NVarChar, 30);
        com.Parameters["@Tm_dept"].Value = sTm_dept;
        com.Parameters.Add("@Tm_Oper", SqlDbType.NVarChar, 30);
        com.Parameters["@Tm_Oper"].Value = sTm_Oper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;
        com.Parameters.Add("@Cr_id", SqlDbType.NVarChar, 30);
        //com.Parameters["@Cr_id"].Value = sCr_id;  
        com.Parameters["@Cr_id"].Value = "C_2006104155146";//-------------------------------------------------------暂时用 

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 增加期初产品明细信息
    /// </summary>
    /// <param>其他入库编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean AddChildOtherEarlySet(string Td_Type, string Tm_No, string Im_No, string sDepot_id,decimal dPrice, int iQty, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddtbhTranD", con);

        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@td_type", SqlDbType.NVarChar, 2);
        com.Parameters["@td_type"].Value = Td_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = Im_No;
        com.Parameters.Add("@depot_id", SqlDbType.NVarChar, 30);
        com.Parameters["@depot_id"].Value = sDepot_id;
        com.Parameters.Add("@Price", SqlDbType.Decimal, 28);
        com.Parameters["@Price"].Value = dPrice;
        com.Parameters.Add("@qty", SqlDbType.Int, 4);
        com.Parameters["@qty"].Value = iQty;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }


    /// <summary>
    /// 修改其他入库明细信息
    /// </summary>
    /// <param>其他入库编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean UpdateChildOtherEarlySet(string Td_Type, string Tm_No, string Im_No, string sDepot_id,decimal dPrice, decimal dQty, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdatetbhTranD", con);

        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@td_type", SqlDbType.NVarChar, 2);
        com.Parameters["@td_type"].Value = Td_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = Im_No;
        com.Parameters.Add("@depot_id", SqlDbType.NVarChar, 30);
        com.Parameters["@depot_id"].Value = sDepot_id;
        com.Parameters.Add("@Price", SqlDbType.Decimal, 28);
        com.Parameters["@Price"].Value = dPrice;
        com.Parameters.Add("@qty", SqlDbType.Decimal, 28);
        com.Parameters["@qty"].Value = dQty;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    public Boolean AddOrUpdateOtherEarlySet(string Td_Type, string Tm_No, string Im_No, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddtbhTranD", con);

        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@td_type", SqlDbType.NVarChar, 2);
        com.Parameters["@td_type"].Value = Td_Type;
        com.Parameters.Add("@tm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@tm_no"].Value = Tm_No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = Im_No;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;
        //com.Parameters.Add("@operate_type", SqlDbType.Int);
        //com.Parameters["@operate_type"].Value = OperateType;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}
